package com.download.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.download.backstage.entity.ThirdAccount;
import com.download.web.api.ApiRequestService;
import com.download.web.entity.SysThirdAccount;
import com.download.web.entity.SysThirdUser;
import com.download.web.entity.SysUser;
import com.download.web.entity.model.Page;
import com.download.web.entity.model.Result;
import com.download.web.mapper.SysThirdAccountMapper;
import com.download.web.service.ISysThirdAccountService;
import com.download.web.service.ISysThirdUserService;
import com.download.web.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: cmy
 * @create: 2019-02-28 14:15
 **/
@Service
public class SysThirdAccountServiceImpl  extends ServiceImpl<SysThirdAccountMapper, SysThirdAccount> implements ISysThirdAccountService {

    @Resource
    SysThirdAccountMapper thirdAccountMapper;

    @Autowired
    private ApiRequestService apiRequestService;
    @Autowired
    private ISysThirdUserService thirdUserService;
    @Autowired
    private ISysUserService userService;

    @Override
    public Page<ThirdAccount> thirdAccountListPage(Integer pageIndex, Integer pageSize) {
        List<ThirdAccount> thirdAccounts=thirdAccountMapper.thirdAccountsByPage(pageIndex*pageSize,pageSize);
        return new Page<>(thirdAccounts,thirdAccountMapper.thirdAccountsCount(),pageIndex,pageSize);
    }

    @Override
    public Result addThirdAccount(SysThirdAccount thirdAccount){
        String errMsg=checkSettings(thirdAccount);
        if (errMsg!=null){
            return new Result(null,errMsg);
        }
        save(thirdAccount);
        return new Result(thirdAccount.getId());
    }
    @Override
    public Result updateThirdAccount(SysThirdAccount thirdAccount){
        String errMsg=checkSettings(thirdAccount);
        if (errMsg!=null){
            return new Result(null,errMsg);
        }
        updateById(thirdAccount);
        return new Result();
    }

    private String checkSettings(SysThirdAccount thirdAccount){

        SysThirdAccount typeCount=getOne(new QueryWrapper<SysThirdAccount>().eq("type",thirdAccount.getType()));
        if (typeCount!=null&&!thirdAccount.getId().equals(typeCount.getId())){
            return "这个登录方式的类型已经存在";
        }
        return  null;
    }

    @Override
    public Map<String, Object> weiboLogin(String code) {
        Map<String,Object> userMap=new HashMap<>();
        SysThirdAccount thirdAccount = thirdAccountMapper.selectOne(new QueryWrapper<SysThirdAccount>().eq("type", 17));
        Map<String, Object> tokenMap = apiRequestService.userToken(code, thirdAccount);
        String accessToken = tokenMap.get("access_token").toString();
        SysThirdUser thirdUser = thirdUserService.getOne(new QueryWrapper<SysThirdUser>().eq("third_user_id", tokenMap.get("uid").toString()));
        //判断是否已经有第三方账号
        if (thirdUser == null) {
            Map<String,Object> wbUserInfo= apiRequestService.weiboUserInfo(accessToken,tokenMap.get("uid").toString());
            SysThirdUser thirdUserAdd=new SysThirdUser(17,wbUserInfo.get("screen_name").toString(),null,wbUserInfo.get("id").toString());
            userMap.put("thirdUser",thirdUserAdd);
        }else {
            userMap.put("thirdUser",thirdUser);
            SysUser user=userService.getById(thirdUser.getUserId());
            userMap.put("user",user);
        }
        return userMap;
    }

    @Override
    public Map<String, Object> qqLoginCallback(String code) {
        Map<String,Object> userMap=new HashMap<>();
        SysThirdAccount thirdAccount = thirdAccountMapper.selectOne(new QueryWrapper<SysThirdAccount>().eq("type", 18));
        Map<String, Object> tokenMap = apiRequestService.qquserToken(code, thirdAccount);
        String accessToken = tokenMap.get("access_token").toString();
        Map<String, Object> openId = apiRequestService.qqUserOpenId(tokenMap.get("access_token").toString());
        SysThirdUser thirdUser = thirdUserService.getOne(new QueryWrapper<SysThirdUser>().eq("third_user_id", openId.get("openid").toString()));
        //判断是否已经有第三方账号
        if (thirdUser == null) {
            Map<String,Object> qqUserInfo= apiRequestService.qqUserInfo(accessToken,thirdAccount.getAppid(),openId.get("openid").toString());
            SysThirdUser thirdUserAdd=new SysThirdUser(17,qqUserInfo.get("nickname").toString(),null,openId.get("openid").toString());
            userMap.put("thirdUser",thirdUserAdd);
        }else {
            userMap.put("thirdUser",thirdUser);
            SysUser user=userService.getById(thirdUser.getUserId());
            userMap.put("user",user);
        }
        return userMap;
    }
}
